<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Sends a message to a socket, whether it is connected or not</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.socket-sendto" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">socket_sendto</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">socket_sendto</span> &mdash; <span class="dc-title">Sends a message to a socket, whether it is connected or not </span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.socket-sendto-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>socket_sendto</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$buf</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$len</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$addr</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$port</code><span class="initializer"> = 0</span></span>
  ] ) : <span class="type">int</span></div>

  <p class="para rdfs-comment">
   The function <span class="function"><strong>socket_sendto()</strong></span> sends
   <code class="parameter">len</code> bytes from <code class="parameter">buf</code>
   through the socket <code class="parameter">socket</code> to the
   <code class="parameter">port</code> at the address <code class="parameter">addr</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.socket-sendto-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">socket</code></dt>

     <dd>

      <p class="para">
       A valid socket resource created using <span class="function"><a href="socket_create.html" class="function">socket_create()</a></span>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">buf</code></dt>

     <dd>

      <p class="para">
       The sent data will be taken from buffer <code class="parameter">buf</code>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">len</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">len</code> bytes from <code class="parameter">buf</code> will be
       sent.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
       The value of <code class="parameter">flags</code> can be any combination of 
       the following flags, joined with the binary OR (<em>|</em>)
       operator.
       <table class="doctable table">
        <caption><strong>Possible values for <code class="parameter">flags</code></strong></caption>
        
         <tbody class="tbody">
          <tr>
           <td><strong><code>MSG_OOB</code></strong></td>
           <td>
            Send OOB (out-of-band) data.
           </td>
          </tr>

          <tr>
           <td><strong><code>MSG_EOR</code></strong></td>
           <td>
            Indicate a record mark. The sent data completes the record.
           </td>
          </tr>

          <tr>
           <td><strong><code>MSG_EOF</code></strong></td>
           <td>
            Close the sender side of the socket and include an appropriate
            notification of this at the end of the sent data. The sent data
            completes the transaction.
           </td>
          </tr>

          <tr>
           <td><strong><code>MSG_DONTROUTE</code></strong></td>
           <td>
            Bypass routing, use direct interface.
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>

    
    
     <dt>
<code class="parameter">addr</code></dt>

     <dd>

      <p class="para">
       IP address of the remote host.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">port</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">port</code> is the remote port number at which the data
       will be sent.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.socket-sendto-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>socket_sendto()</strong></span> returns the number of bytes sent to the
   remote host, or <strong><code>FALSE</code></strong> if an error occurred.
  </p>
 </div>



 <div class="refsect1 examples" id="refsect1-function.socket-sendto-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5605">
    <p><strong>Example #1 <span class="function"><strong>socket_sendto()</strong></span> Example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;$sock&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOCK_DGRAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_UDP</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$msg&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Ping&nbsp;!"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$len&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$msg</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">socket_sendto</span><span style="color: #007700">(</span><span style="color: #0000BB">$sock</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$msg</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$len</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'127.0.0.1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1223</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$sock</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.socket-sendto-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="socket_send.html" class="function" rel="rdfs-seeAlso">socket_send()</a> - Sends data to a connected socket</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>